package com.whu.waterworks.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate4.HibernateTemplate;

import com.whu.waterworks.dao.QualityRecordDataDao;
import com.whu.waterworks.model.Antisseptic;
import com.whu.waterworks.model.QualityNonNormol;
import com.whu.waterworks.model.QualityNormol;
import com.whu.waterworks.model.WaterPlant;

/**
 * @author ZhouBin
 * @date 2016-4-12 上午9:25:06
 */
public class QualityRecordDataDaoImpl implements QualityRecordDataDao {
	private HibernateTemplate hibernateTemplate;
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@Override
	public List<QualityNormol> getQualityNormols(String waterPlantId,
			String starttime, String endtime) {
		@SuppressWarnings("unchecked")
		ArrayList<QualityNormol> qns=(ArrayList<QualityNormol>) hibernateTemplate.find(
				"from QualityNormol qn where qn.waterplantId='"+waterPlantId+"' and qn.time between '"+
						starttime+"' and '"+endtime+"'");
		return qns;
	}

	@Override
	public List<QualityNonNormol> getQualityNonNormols(String waterPlantId,
			String starttime, String endtime) {
		@SuppressWarnings("unchecked")
		ArrayList<QualityNonNormol> qnns=(ArrayList<QualityNonNormol>) hibernateTemplate.find("from QualityNonNormol qnn where qnn.waterplantId='"+waterPlantId+"' and qnn.time between '"+
				starttime+"' and '"+endtime+"'");
		return qnns;
	}

	@Override
	public List<Antisseptic> getAntisseptics(String waterPlantId,
			String startTime, String endTime) {
		@SuppressWarnings("unchecked")
		ArrayList<Antisseptic> antisseptics=(ArrayList<Antisseptic>) hibernateTemplate.find("from Antisseptic ap where ap.waterplantId='"+waterPlantId+"' and ap.time between '"+
	    		 startTime+"' and '"+endTime+"'");
			return antisseptics;
	}

	@Override
	public String getQualityNormolStandardValue(String testItem) {
		// TODO Auto-generated method stub
		List<String> list= (List<String>)hibernateTemplate.find("SELECT _QualityNormol."+testItem+" from QualityNormol _QualityNormol where _QualityNormol.type='1'");
		if(null == list || list.size() ==0){
			return null;			
		}else {
			String s = String.valueOf(list.get(0)) ;
			return s;
		}	
	}

	@Override
	public String getQualityNonNormolStandardValue(String testItem) {
		// TODO Auto-generated method stub
		List<String> list= (List<String>)hibernateTemplate.find("SELECT _QualityNonNormol."+testItem+" from QualityNonNormol _QualityNonNormol where _QualityNonNormol.type='1'");
		if(null == list || list.size() ==0){
			return null;			
		}else {
			String s = String.valueOf(list.get(0)) ;
			return s;
		}
	}

	@Override
	public String getAntissepticStandardValue(String testItem) {
		// TODO Auto-generated method stub
		List<String> list= (List<String>)hibernateTemplate.find("SELECT _Antisseptic."+testItem+" from Antisseptic _Antisseptic where _Antisseptic.type='1'");
		if(null == list || list.size() ==0){
			return null;			
		}else {
			String s = String.valueOf(list.get(0)) ;
			return s;
		}	
	}

	@Override
	public String getWaterPlantById(String waterPlantCode) {
		// TODO Auto-generated method stub
		List<WaterPlant> waterPlants =(List<WaterPlant>) hibernateTemplate.find("from WaterPlant _WaterPlant where _WaterPlant.waterPlantCode='"+waterPlantCode+"'");
		if (waterPlants==null) {
			return null;
		}
		else {		
			return waterPlants.get(0).getWaterPlantName();
		}
	}

	@Override
	public List<QualityNormol> getQualityNormolByIdAndTime(String waterPlantId,
			String time) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		ArrayList<QualityNormol> rs =(ArrayList<QualityNormol>) hibernateTemplate.find("" +
				"from QualityNormol _QualityNormol where _QualityNormol.waterplantId='"+waterPlantId+"' and _QualityNormol.time='"+time+"'");
		if(null!=rs && rs.size()>0){
			return rs;
		}
		return null;
	}

	@Override
	public List<QualityNonNormol> getQualityNonNormolByIdAndTime(
			String waterPlantId, String time) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		ArrayList<QualityNonNormol> rs =(ArrayList<QualityNonNormol>) hibernateTemplate.find("" +
				"from QualityNonNormol _QualityNonNormol where _QualityNonNormol.waterplantId='"+waterPlantId+"' and _QualityNonNormol.time='"+time+"'");
		if(null!=rs && rs.size()>0){
			return rs;
		}
		return null;
	}

	@Override
	public List<Antisseptic> getAntissepticByIdAndTime(String waterPlantId,
			String time) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		ArrayList<Antisseptic> rs =(ArrayList<Antisseptic>) hibernateTemplate.find("" +
				"from Antisseptic _Antisseptic where _Antisseptic.waterplantId='"+waterPlantId+"' and _Antisseptic.time='"+time+"'");
		if(null!=rs && rs.size()>0){
			return rs;
		}
		return null;
	}

}
